package edu.fudan.jwxt.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.fudan.jwxt.Exception.ServiceException;
import edu.fudan.jwxt.common.Constants;
import edu.fudan.jwxt.dao.CourseDao;
import edu.fudan.jwxt.dto.CourseDto;
import edu.fudan.jwxt.entity.*;
import edu.fudan.jwxt.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static edu.fudan.jwxt.utils.CourseUtil.generateCourseString;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseDao, Course> implements CourseService {
    @Autowired
    private CourseDao courseDao;

    @Autowired
    private UserService userService;

    @Autowired
    private TeacherCourseService teacherCourseService;

    @Autowired
    private CourseTimeService courseTimeService;

    @Autowired
    private CollegeService collegeService;

    @Autowired
    private MajorService majorService;

    @Autowired
    private ClassroomService classroomService;

    @Autowired
    private ClassTimeService classTimeService;



    @Override
    public boolean save(Course course){

        if (!selectByFirstString("course_id",course.getCourseId()).isEmpty())
            throw new ServiceException(Constants.CODE_600,"添加相同大课代码");
        courseDao.insert(course);
        return true;
    }

    @Override
    public void delete(Course course){
        if (selectByFirstString("course_id",course.getCourseId()).isEmpty())
            throw new ServiceException(Constants.CODE_600,"删除不存在课程");
        Map<String,Object> map = new HashMap<>();
        map.put("course_id",course.getCourseId());
        courseDao.deleteByMap(map);
    }

    @Override
    public void update(Course course){
        if (selectByFirstString("course_id",course.getCourseId()).isEmpty())
            throw new ServiceException(Constants.CODE_600,"修改不存在课程");
        delete(course);
        save(course);
    }

    @Override
    public List<Course> selectByCourseId(String courseId){
        List<Course> Ans=selectByFirstString("course_id",courseId);
        if(Ans.isEmpty())
            throw new ServiceException(Constants.CODE_600,"查询不存在课程");
        if(Ans.size()>1)
            throw new ServiceException(Constants.CODE_600,"系统内部相同编号课程出现两个,请及时删除其中之一");
        return Ans;
    }

    // 必须不作检查,才能兼容后面的东西,但是这个函数又太好用了，好用到我想做个检查
    @Override
    public List<Course> selectByFirstString(String firstString, Object ObjectNumber){
        Map<String,Object> map = new HashMap<>();
        map.put(firstString,ObjectNumber);
        List<Course> templist = courseDao.selectByMap(map);
        return templist;
    }

    // 把同一个course_id的所有平行班全部拉出来
    @Override
    public List<TeacherCourse> Transfer(Course course){
        return teacherCourseService.selectByCourseId(course.getCourseId());
    }

    // 输入course,输出其对应的所有平行班List<CourseDto>
    // 本质:通过courseId查询
    @Override
    public List<CourseDto> TransferToCourseDto(String courseId){
        Course course=selectByCourseId(courseId).get(0);

        List<TeacherCourse> teacherCourseList=teacherCourseService.selectByCourseId(courseId);
        List<CourseDto> courseDtoList=new ArrayList<>();
        for (TeacherCourse teacherCourse:teacherCourseList
             ) {
            CourseDto courseDto=new CourseDto(course);
            List<CourseTime> courseTimeList=courseTimeService.selectByClassId(teacherCourse.getClassId());
            courseDto.setExactClassTime(courseTimeService.Transfer(courseTimeList));
            courseDto.setTimeString(courseTimeService.TransferToTs(courseTimeList));
            courseDto.setTeacherName(userService.getNameByUid(teacherCourse.getTeacherId()));
            courseDto.setTeacherId(teacherCourse.getTeacherId());
            courseDto.setMajorName(majorService.getNameById(course.getMajorId()));
            courseDto.setCollegeName(collegeService.getNameById(course.getCollegeId()));
            courseDto.setCourseString(generateCourseString(course.getCourseId(),teacherCourse.getCourseAid()));
            courseDto.setClassRoom(classroomService.getNameById(teacherCourse.getClassroomId()));
            courseDto.setClassRoomID(teacherCourse.getClassroomId());
            courseDto.setCourseTeacherAid(teacherCourse.getClassId());

            courseDtoList.add(courseDto);
        }
        return courseDtoList;
    }

    @Override
    public CourseDto TransferToCourseDto(String courseId, Integer classId){
        List<CourseDto> courseDtoList=TransferToCourseDto(courseId);
        TeacherCourse teacherCourse=teacherCourseService.selectByCourseIdClassId(courseId,classId);
        Course course=selectByCourseId(courseId).get(0);
        CourseDto courseDto=new CourseDto(course);
        List<CourseTime> courseTimeList=courseTimeService.selectByClassId(teacherCourse.getClassId());
        courseDto.setExactClassTime(courseTimeService.Transfer(courseTimeList));
        courseDto.setTimeString(courseTimeService.TransferToTs(courseTimeList));
        courseDto.setTeacherName(userService.getNameByUid(teacherCourse.getTeacherId()));
        courseDto.setTeacherId(teacherCourse.getTeacherId());
        courseDto.setMajorName(majorService.getNameById(course.getMajorId()));
        courseDto.setCollegeName(collegeService.getNameById(course.getCollegeId()));
        courseDto.setCourseString(generateCourseString(course.getCourseId(),teacherCourse.getCourseAid()));
        courseDto.setClassRoom(classroomService.getNameById(teacherCourse.getClassroomId()));
        courseDto.setClassRoomID(teacherCourse.getClassroomId());
        courseDto.setCourseTeacherAid(teacherCourse.getClassId());
        return courseDto;
    }

    @Override
    public void checkRoomTimeOccupy(Course tempCourse) {

    }


}




